package org.xmlprocess.LircClient.http;

import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class HttpHandler {

	private DefaultHttpClient client = null;

	public String retrieve(String url) {
		// Log.w(getClass().getSimpleName(),
		// "retrieve(String url) called for URL " + url);
		HttpGet getRequest = new HttpGet(url);
		try {

			HttpResponse getResponse = getClient().execute(getRequest);
			final int statusCode = getResponse.getStatusLine().getStatusCode();

			if (statusCode != HttpStatus.SC_OK) {
				Log.w(getClass().getSimpleName(), "Error " + statusCode
						+ " for URL " + url);
				return null;
			}

			HttpEntity getResponseEntity = getResponse.getEntity();

			if (getResponseEntity != null) {
				return EntityUtils.toString(getResponseEntity);
			}

		} catch (IOException e) {
			getRequest.abort();
			Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
		}

		return null;

	}

	public InputStream retrieveStream(String url) {
		// Log.w(getClass().getSimpleName(), "retrieveStream called for URL " +
		// url);
		HttpGet getRequest = new HttpGet(url);

		try {

			HttpResponse getResponse = getClient().execute(getRequest);
			final int statusCode = getResponse.getStatusLine().getStatusCode();

			if (statusCode != HttpStatus.SC_OK) {
				Log.w(getClass().getSimpleName(), "Error " + statusCode
						+ " for URL " + url);
				return null;
			}

			HttpEntity getResponseEntity = getResponse.getEntity();
			return getResponseEntity.getContent();

		}

		catch (Exception e) {
			getRequest.abort();
			// send app to error page

			Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
		}

		return null;

	}

	public DefaultHttpClient getClient() {
		if (client == null) {
			HttpParams httpParameters = new BasicHttpParams();

			// Connection timeout
			HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
			// Socket timeout
			HttpConnectionParams.setSoTimeout(httpParameters, 3000);

			client = new DefaultHttpClient(httpParameters);
		}

		return client;
	}

	public void setClient(DefaultHttpClient client) {
		this.client = client;
	}

}
